﻿using System.Linq;
using System.Collections.Generic;
using Red.Core;

namespace Red.Controls.Depot.ViewModels
{
    public class ChangelistViewModel : ViewModel
    {
        #region Data

        private string _description;
        private List<ChangelistFileInfo> _files;
        private bool? _allChecked = true;

        #endregion Data

        #region Properties

        public string Description
        {
            get { return _description; }
            set
            {
                if ( _description == value )
                    return;

                _description = value;
                OnPropertyChanged();
            }
        }

        public List<ChangelistFileInfo> Files
        {
            get { return _files; }
            set
            {
                if ( _files == value )
                    return;

                _files = value;
                OnPropertyChanged();
            }
        }

        public bool? AllChecked
        {
            get
            {
                return _allChecked;
            }

            set
            {
                /*
                 * Checkbox is threestate but user is switching only between checked/unchecked state.
                 * This is why we are filtering out null value.
                 */
                var newValue = value ?? false;
                if ( _allChecked == newValue )
                    return;

                _allChecked = newValue;
                _files.ForEach( item => item.IsChecked = newValue );               
            }
        }

        #endregion Properties

        #region Public methods

        public void RefreshSelectionState()
        {
            _allChecked = null;

            var allChecked = _files.All( item => item.IsChecked );
            if ( allChecked )
                _allChecked = true;

            var allUnchecked = _files.All( item => !item.IsChecked );
            if ( allUnchecked )
                _allChecked = false;

            OnPropertyChanged( "AllChecked" );
        }

        #endregion Public methods
    }
}
